/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package scrolllistexample;

import javax.swing.*;
import javax.swing.event.*;

/**
 *
 * @author eid
 */
public class ScrollListPanel extends JPanel {


    private JList monthList;           // The months
    private JTextField selectedMonth;  // The selected month
    private JLabel label;              // A message

    private JScrollPane scrollPane;    // A scroll pane
    

    // The following array holds the values that will
    // be displayed in the monthList list component.
    private String[] months = { "January", "February",
            "March", "April", "May", "June", "July",
            "August", "September", "October", "November",
            "December" };

    public ScrollListPanel() {

      monthList = new JList(months);

      // Set the selection mode to single selection.
      monthList.setSelectionMode(
                ListSelectionModel.SINGLE_SELECTION);

      // Register the list selection listener.
      monthList.addListSelectionListener(
                                 new ScrollListListener());

      // Add the list to the panel.
      add(monthList);


      // Set the number of visible rows to 6.
      monthList.setVisibleRowCount(6);

      // Add the list to a scroll pane.
      scrollPane = new JScrollPane(monthList);

      // Add the scroll pane to the panel.
      add(scrollPane);


      // Create the label.
      label = new JLabel("You selected: ");

      // Create the text field.
      selectedMonth = new JTextField(10);

      // Make the text field uneditable.
      selectedMonth.setEditable(true);

      // Add the label and text field to the panel.
      add(label);
      add(selectedMonth);
    }


    private class ScrollListListener implements ListSelectionListener {
      public void valueChanged(ListSelectionEvent e)
      {
         // Get the selected month.
         String selection =
               (String) monthList.getSelectedValue();

         // Put the selected month in the text field.
         selectedMonth.setText(selection);
      }
   }



}
